package com.yupi.springbootinit.GuaVa;

import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Objects;
import java.util.concurrent.TimeUnit;


/**
 * 重试框架管理类
 */
@Configuration
@Slf4j
public class RetryManager<T> {

    @Bean
    public Retryer<T> RetryManager(){
        int i = 0;
        log.info("当前重试次数为：{}",i);
        //1:RetryerBuilder 构造重试器
        return  RetryerBuilder.<T>newBuilder()
                //todo 具体异常特殊处理
                //2：设置任何异常都重试
                .retryIfException()
                .retryIfResult(Objects::isNull)
                //3:设置等待时间
                .withWaitStrategy(WaitStrategies.incrementingWait( 10, TimeUnit.SECONDS,10, java.util.concurrent.TimeUnit.SECONDS ))
                //4：设置重试次数
                .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .build();
    }
}
